package com.quizzer.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.quizzer.common.utils.DateUtils;
import com.quizzer.system.convert.SubjectConverter;
import com.quizzer.system.dto.question.subject.SubjectAddDto;
import com.quizzer.system.dto.question.subject.SubjectOutputDto;
import com.quizzer.system.dto.question.subject.SubjectQueryDto;
import com.quizzer.system.dto.question.subject.SubjectUpdateDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.quizzer.system.mapper.SubjectMapper;
import com.quizzer.system.domain.Subject;
import com.quizzer.system.service.ISubjectService;

/**
 * 大类别Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-07
 */
@Service
public class SubjectServiceImpl implements ISubjectService
{
    @Autowired
    private SubjectMapper subjectMapper;

    /**
     * 查询大类别
     *
     * @param id 大类别主键
     * @return 大类别
     */
    @Override
    public SubjectOutputDto selectSubjectById(Long id)
    {
        Subject subject = subjectMapper.selectSubjectById(id);
        return selectOne(subject);
    }

    private SubjectOutputDto selectOne(Subject subject) {
        return SubjectConverter.toOutputDto(subject);
    }

    /**
     * 查询大类别列表
     *
     * @param queryDto 大类别
     * @return 大类别
     */
    @Override
    public List<SubjectOutputDto> selectSubjectList(SubjectQueryDto queryDto)
    {
        Subject subject = SubjectConverter.toEntity(queryDto);
        List<Subject> subjects = subjectMapper.selectSubjectList(subject);
        List<SubjectOutputDto> outputDtoList = new ArrayList<>();
        for (Subject item : subjects) {
            outputDtoList.add(selectOne(item));
        }
        return outputDtoList;
    }

    /**
     * 新增大类别
     *
     * @param addDto 大类别
     * @return 结果
     */
    @Override
    public int insertSubject(SubjectAddDto addDto)
    {
        Subject subject = SubjectConverter.toEntity(addDto);
        return subjectMapper.insertSubject(subject);
    }

    /**
     * 修改大类别
     *
     * @param updateDto 大类别
     * @return 结果
     */
    @Override
    public int updateSubject(SubjectUpdateDto updateDto)
    {
        Subject subject = SubjectConverter.toEntity(updateDto);
        return subjectMapper.updateSubject(subject);
    }

    /**
     * 批量删除大类别
     *
     * @param ids 需要删除的大类别主键
     * @return 结果
     */
    @Override
    public int deleteSubjectByIds(Long[] ids)
    {
        return subjectMapper.deleteSubjectByIds(ids);
    }

    /**
     * 删除大类别信息
     *
     * @param id 大类别主键
     * @return 结果
     */
    @Override
    public int deleteSubjectById(Long id)
    {
        return subjectMapper.deleteSubjectById(id);
    }
}
